﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_col_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlParentCol.DataSource = dal.GetColtd(int.Parse(Session["ColtdId"].ToString()), true);
            this.ddlParentCol.DataBind();
            this.ddlParentCol.Items.Insert(0, new ListItem("--顶层--", "0"));
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_face_m_coltd(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetColtd(int.Parse(this.hidId.Value), false);
                if (dt.Rows.Count > 0)
                {
                    this.ddlParentCol.Items.FindByValue(dt.Rows[0]["up_coltd_code"].ToString()).Selected = true;
                    this.txtColFullName.Text = dt.Rows[0]["coltd_name"].ToString();
                    this.txtColSimpleName.Text = dt.Rows[0]["coltd_simplename"].ToString();
                    this.txtSort.Text = dt.Rows[0]["self_sortid"].ToString();
                    this.hidLayer.Value = dt.Rows[0]["self_layer"].ToString();
                    this.hidCode.Value = dt.Rows[0]["self_all_code"].ToString();
                    this.hidUpId.Value = dt.Rows[0]["up_coltd_code"].ToString();
                }
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            string sPColtd = this.ddlParentCol.SelectedValue;
            string sFullName = this.txtColFullName.Text.Trim();
            string sSimpleName = this.txtColSimpleName.Text.Trim();
            string sSort = this.txtSort.Text.Trim();
            int iSort = 0;
            if (sFullName == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "全称不能为空！");
                return;
            }
            
            if (sSort != string.Empty)
            {
                if (!int.TryParse(sSort, out iSort))
                {
                    PM.Web.Common.JsAlert(this, "排序为数字！");
                    return;
                }
            }
            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_coltd(0, int.Parse(sPColtd), sFullName, sSimpleName, 0, iSort, string.Empty,string.Empty, bool.Parse(this.ddlStatus.SelectedValue)))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！",true);
                }
            }
            else
            {
                if (dal.Modify_face_m_coltd(int.Parse(this.hidId.Value), int.Parse(this.hidLayer.Value), int.Parse(this.hidUpId.Value), sFullName, sSimpleName, 0, iSort, this.hidCode.Value, string.Empty, bool.Parse(this.ddlStatus.SelectedValue)))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！",true);
                }
            }
        }
    }
}